package me.luger.project.fof.controller.api;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.dao.nutz.entity.page.Pagination;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.fof.entity.FundInfo;
import me.luger.project.fof.service.FundCollectService;
import me.luger.project.fof.service.FundInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * FundCollect controller
 */
@Controller
@RequestMapping("/api/fof/fundCollect")
public class FundCollectApiController extends BaseController {

    @Autowired
    private FundCollectService fundCollectService;

    @Autowired
    private FundInfoService fundInfoService;

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/list", name = "获取某个人收藏的基金")
    @ResponseBody
    public Object list(Integer uid,
                       @RequestParam(defaultValue = "1") Integer pn,
                       @RequestParam(defaultValue = "20") Integer rn) {
        Pagination<FundInfo> fundPageList = fundInfoService.myCollect(uid,pn,rn);
        return Result.success(fundPageList);
    }

    @RequestMapping(value = "/collect", name = "收藏某个基金")
    @ResponseBody
    public Object collect(Integer uid,String fundExtCode) {
        boolean isCollect = fundCollectService.isCollect(uid,fundExtCode);
        if(isCollect){
            return Result.info(2,"已收藏");
        }
        boolean success = fundCollectService.collect(uid,fundExtCode);
        if(success){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @RequestMapping(value = "/unCollect", name = "取消收藏某个基金")
    @ResponseBody
    public Object unCollect(Integer uid,String fundExtCode) {
        boolean success = fundCollectService.unCollect(uid,fundExtCode);
        if(success){
            return Result.success();
        }else {
            return Result.error();
        }
    }
}
